#include "StdAfx.h"
#include "PopupEventThread.h"
#include "popup.h"
#include "popupdlg.h"
/*
#include "UserMsgs.h"
#include "OtherFunctions.h"
#include "TaskbarNotifier.h"
#include "ResourceEntry.h"
#include "SenderDialog.h"
#include "PopupClient.h"
*/
#include <vector>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////
///PopupEventThread
IMPLEMENT_DYNCREATE(PopupEventThread, CWinThread)

PopupEventThread::PopupEventThread() 
 : m_popupapp((CpopupApp*) AfxGetApp()), 
   m_popupdlg(m_popupapp->popupdlg)
{
}

BOOL PopupEventThread::InitInstance()
{
	return TRUE;
}

int PopupEventThread::Run()
{
	PopupMessageList *_messageList = &(m_popupdlg->m_incomingMessages);
	CSingleLock _lock(&CpopupDlg::s_eventmutex);
	
  while (true)
  {
	_lock.Lock();
	if (!_messageList->empty())
    {
		_lock.Unlock();
		while (m_popupdlg->isMuted() == true)
		{
			Sleep (3000);
		}
		_lock.Lock();
		PopupMessageList::iterator _it = _messageList->begin();
		PopupMessageInfo _info = (*_it);
		_messageList->erase(_it);
		_lock.Unlock();
		if (m_popupdlg->isReadyToDisplayNextMessage())
		{
			m_popupdlg->displayMessage(_info);
		}
    }
	_lock.Unlock();
	Sleep(1000);
  }    

  return 0;
}
